<?php
define('ROOT_DIR', dirname(dirname(__FILE__)));
require ROOT_DIR.'/library/Xiachufang/util/Request.php';
$recipeId = Request::getInt("recipeId");
$type = $_GET["type"];
$id = $_GET[$type."Id"];
$size = $_GET["size"];
$filePath = ROOT_DIR."/pictures/".$type."/".$id.".jpg";

if (!file_exists($filePath)) {
	header("Content-type:image/gif");
	$filePath = ROOT_DIR."/pictures/".$type."/default.gif";
} else {
	if ($size) {
		$thumbPath = ROOT_DIR."/pictures/".$type."/".$size."/".$id.".jpg";
		if (!file_exists($thumbPath)) {
			$width = $height = 0;
			switch($size) {
				case "small":
					$width = 100;
					$height = 75;
					break;
				case "middle":
					$width = 140;
					$height = 110;
					break;
				case "subbig":
					$width = 240;
					$height = NULL;
					break;
				case "square":
					$width = $height = 110;
					break;
			}
			if ($width > 0) {
				require ROOT_DIR.'/library/Xiachufang/util/Util.php';
				Util::thumbPicture($filePath, $thumbPath, $width, $height);
			}
		}
		$filePath = $thumbPath;
	}

	header("Content-type:image/jpeg");
}
$mtime = filemtime($filePath);
$eTag = '"'.md5($mtime).'"';
header('Etag: '.$eTag);
if ($eTag == $_SERVER['HTTP_IF_NONE_MATCH']) {
	header("HTTP/1.1 304 Not modified");
	return;
} else {
	//header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mtime)." GMT");
	//header("Cache-Control:max-age:2592000,public");
	header("Expires: ".gmdate("D, d M Y H:i:s", time() + 24*3600)." GMT");
	$fp = fopen($filePath,'rb');
	$length = filesize($filePath);
	if($fp != false) {
		header("Content-Length:".$length);
		$contents = fread($fp,$length);
		fclose($fp);
	}
	print $contents;
}
?>